package com.utooo.service.purchase;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.utooo.dao.purchase.InvoiceAssociationDetailMapper;
import com.utooo.dao.purchase.OrderAssociationDetailMapper;
import com.utooo.dao.purchase.OrderInvoiceAssociationMapper;
import com.utooo.dao.supplier.SupplierOrderInvoiceAssociationMapper;
import com.utooo.pojo.domain.purchase.order.InvoiceAssociationDetailDO;
import com.utooo.pojo.domain.purchase.order.OrderAssociationDetailDO;
import com.utooo.pojo.domain.purchase.order.OrderInvoiceAssociationDO;
import com.utooo.pojo.domain.purchase.order.RefundApplicationDO;
import com.utooo.pojo.domain.supplier.settlement.SupplierOrderInvoiceAssociationDO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static com.utooo.constant.Constans.IGNORED_PROPERTIES;

@Service
@Slf4j
@RequiredArgsConstructor
public class OrderInvoiceAssociationServiceImpl extends ServiceImpl<OrderInvoiceAssociationMapper, OrderInvoiceAssociationDO>
        implements OrderInvoiceAssociationService {

    private final SupplierOrderInvoiceAssociationMapper supplierOrderInvoiceAssociationMapper;
    private final OrderAssociationDetailMapper orderAssociationDetailMapper;
    private final InvoiceAssociationDetailMapper invoiceAssociationDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrderInvoiceAssociation(String id) {
        OrderInvoiceAssociationDO associationDO = this.getById(id);
        if (associationDO == null) {
            log.error("order invoice association is null");
            return;
        }
        SupplierOrderInvoiceAssociationDO target = new SupplierOrderInvoiceAssociationDO();
        BeanUtil.copyProperties(associationDO, target, IGNORED_PROPERTIES);
        target.setPurchaseOrderInvoiceId(id);
        supplierOrderInvoiceAssociationMapper.insert(target);
        String associationId = target.getId();

        List<OrderAssociationDetailDO> orderAssociationDetails =
                orderAssociationDetailMapper.selectList(new LambdaQueryWrapper<OrderAssociationDetailDO>()
                .eq(OrderAssociationDetailDO::getForeignId, id));

        if (CollUtil.isNotEmpty(orderAssociationDetails)) {
            CopyOptions copyOptions = CopyOptions.create().setIgnoreProperties(IGNORED_PROPERTIES);
            List<OrderAssociationDetailDO> details =
                    BeanUtil.copyToList(orderAssociationDetails, OrderAssociationDetailDO.class, copyOptions);
            details.forEach(i -> i.setForeignId(associationId));
            orderAssociationDetailMapper.insert(details);
        }

        List<InvoiceAssociationDetailDO> invoiceAssociationDetails =
                invoiceAssociationDetailMapper.selectList(new LambdaQueryWrapper<InvoiceAssociationDetailDO>()
                        .eq(InvoiceAssociationDetailDO::getForeignId, id));
        if (CollUtil.isNotEmpty(invoiceAssociationDetails)) {
            CopyOptions copyOptions = CopyOptions.create().setIgnoreProperties(IGNORED_PROPERTIES);
            List<InvoiceAssociationDetailDO> details =
                    BeanUtil.copyToList(invoiceAssociationDetails, InvoiceAssociationDetailDO.class, copyOptions);
            details.forEach(i -> i.setForeignId(associationId));
            invoiceAssociationDetailMapper.insert(details);
        }
    }

    @Override
    public String getOrderInvoiceAssociationNo() {
        String prefix = "DKP";
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        String datePart = currentDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));

        // 查询当天已有的最大订单号
        LambdaQueryWrapper<OrderInvoiceAssociationDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.likeRight(OrderInvoiceAssociationDO::getInvoiceNo, prefix + datePart);
        queryWrapper.orderByDesc(OrderInvoiceAssociationDO::getInvoiceNo);
        queryWrapper.last("LIMIT 1");
        OrderInvoiceAssociationDO orderInvoiceAssociationDO = this.getOne(queryWrapper);

        // 生成序列号
        int maxSequence = 0;
        if (orderInvoiceAssociationDO != null) {
            String invoiceNo = orderInvoiceAssociationDO.getInvoiceNo();
            maxSequence = Integer.parseInt(invoiceNo.substring((prefix + datePart).length()));
        }
        int nextSequence = maxSequence + 1;
        String sequencePart = String.format("%03d", nextSequence);
        return prefix + datePart + sequencePart;
    }
}